/*
   Copyright (c) 2010 Bryce Alcock

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.com

 */

package com.ecocrypt.network.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Bryce.Alcock
 */
public class BlockingRandomReplyServer {


    public static void main(String args[]) throws Exception{
        int port = (args.length==1)?Integer.parseInt(args[0]):3600;
        ServerSocket ss = new ServerSocket(port);
        while(true){
            Socket sock = ss.accept();
            RandomLengthReplyServer rlrs = new RandomLengthReplyServer(sock);
            rlrs.start();

        }
    }

    private static class RandomLengthReplyServer extends Thread{
        Socket sock;
        InputStream is;
        OutputStream os;
        RandomLengthReplyServer(Socket s) throws Exception{
            sock=s;
            is=sock.getInputStream();
            os=sock.getOutputStream();
        }

        @Override
        public void run() {


                //------------------------------------------------------------------
                //
                //  1.  Sleep for 500 milliseconds seconds to
                //      make sure that the proxy and send some data.
                //  2.  Read from the socket until no bytes are available.
                //  3.  Write a ramdom variable out to the socket.
                //  4.  Then we will right to the socket,
                //  5.  finally, we will read back off the socket.
                //
                //------------------------------------------------------------------

            try {

                Thread.sleep(500);

                while (is.available() > 0) {
                    is.read(new byte[is.available()]);
                    Thread.sleep(10);
                }

                int bytesReturned = (int) (Math.random()*10000);
                for (int i = 0; i < bytesReturned; ++i) {
                    os.write((byte)(Math.random()*1000%63 + 64 ));
                }
                os.flush();
                while (is.available() > 0) {
                    is.read(new byte[is.available()]);
                    Thread.sleep(10);
                }

                os.close();
                is.close();
                sock.close();

            } catch (IOException ex) {
                Logger.getLogger(BlockingRandomReplyServer.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InterruptedException ex) {
                Logger.getLogger(BlockingRandomReplyServer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
